import { createRouter, createWebHashHistory } from "vue-router";
import Login from "../components/Login.vue"; //引入login组件
import Home from "../components/Home.vue";
import Welcome from "../components/Welcome.vue";
import UserList from "../components/admin/UserList.vue";
import Field from "../components/admin/Field.vue"
import Order from "../components/admin/Order.vue"
import Goods from "../components/admin/goods.vue"
import Clothes from "../components/admin/Clothes.vue"
import Util from "../components/admin/Util.vue"
const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: Welcome },
      { path: "/user", component: UserList },
      { path: "/field", component:Field},
      { path: "/clothes", component:Clothes},
      { path: "/util", component:Util},
      { path: "/order", component:Order}
    ],
  },

];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to将要访问
  //from 从哪访问
  // next 接着干 next(url) 重定向到url上，next()继续访问 to的路径
  if (to.path == "/login") return next();
  //获取user
  const userFlag = window.sessionStorage.getItem("user"); //取出当前用户
  if (!userFlag) return next("/login"); //无值返回登录页
  next(); //符合要求 放行
});

export default router;
